﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicViet.Models;

namespace MedicViet.Controllers
{ 
    public class ServicesController : Controller
    {
        private MedicVietEntities db = new MedicVietEntities();
        private DiskFileStore _fileStore = new DiskFileStore();

        //
        // GET: /Services/

        public ViewResult Index()
        {
            var listNew = (from c in db.mvCategories.Where(cat => cat.name == "Services")
                           from p in db.mvNews.Where(news => news.mvCategories.Contains(c))
                           orderby p.dateCreate
                           descending
                           select p).ToList<mvNew>();
            return View(listNew.ToList());
        }

        public ViewResult Details(int id)
        {
            mvNew mvnew = db.mvNews.Single(m => m.id == id);
            return View(mvnew);
        }

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Services/Create

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Guid? thumbPath,string fileName,mvNew mvnew)
        {
            if (Request.Files["thumbPath"] != null)
            {
                thumbPath = _fileStore.SaveUploadedFile(Request.Files["thumbPath"]);
                fileName = Request.Files["thumbPath"].FileName;
            }
            if (ModelState.IsValid)
            {
                mvCategory cat = (from C in db.mvCategories
                                  where C.name == "Services"
                                  select C).FirstOrDefault();
                mvnew.thumbPath = Request.Form["thumbPath_guid"]+".png";
                mvnew.dateCreate = DateTime.Now;
                mvnew.mvCategories.Add(cat);
                mvnew.userId = 1;
                db.mvNews.AddObject(mvnew);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.userId = new SelectList(db.mvUsers, "id", "username", mvnew.userId);
            return View(mvnew);
        }
        
        //
        // GET: /Services/Edit/5
 
        public ActionResult Edit(int id)
        {
            mvNew mvnew = db.mvNews.Single(m => m.id == id);
            ViewBag.userId = new SelectList(db.mvUsers, "id", "username", mvnew.userId);
            return View(mvnew);
        }

        //
        // POST: /Services/Edit/5

        [HttpPost]
        public ActionResult Edit(mvNew mvnew)
        {
            if (ModelState.IsValid)
            {
                mvnew.dateModified = DateTime.Now;
                db.mvNews.Attach(mvnew);
                db.ObjectStateManager.ChangeObjectState(mvnew, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.userId = new SelectList(db.mvUsers, "id", "username", mvnew.userId);
            return View(mvnew);
        }

        //
        // GET: /Services/Delete/5
 
        public ActionResult Delete(int id)
        {
            mvNew mvnew = db.mvNews.Single(m => m.id == id);
            return View(mvnew);
        }

        //
        // POST: /Services/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            mvNew mvnew = db.mvNews.Single(m => m.id == id);
            db.mvNews.DeleteObject(mvnew);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public Guid AsyncUpload()
        {
            return _fileStore.SaveUploadedFile(Request.Files[0]);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}